package com.dotmedia.dboperations;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.dotmedia.core.DatabaseAccessService;
import com.dotmedia.entities.PartTypes;

/**
 * This class is the database operations for <code>PartTypes</code> entity
 * 
 * @author siddharthsaha
 * 
 */
public class PartTypesDatabaseOperations {

	private static Logger logger = Logger.getLogger("dotmedia.dboperations");

	public void persistPartTypesEntity(PartTypes partTypesEntity) {
		logger.info("IN method persistPartTypesEntity() in PartTypesDatabaseOperations");
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		int persistStatus = databaseAccessService.persistEntity(partTypesEntity);
		if (persistStatus == 0) {
			logger.info("The PartTypes entity was successfully persisted");
		} else {
			logger.error("The PartTypes entity was not successfully persisted in the database");
		}
		logger.info("OUT method persistPartTypesEntity() in PartTypesDatabaseOperations");
	}

	public void removePartTypesEntity(PartTypes partTypesEntity) {
		logger.info("IN method removePartTypesEntity() in PartTypesDatabaseOperations");
		logger.info("Removing a PartTypes entity with the id: " + partTypesEntity.getPartTypeId());
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		int removeStatus = databaseAccessService.removeEntity(partTypesEntity, partTypesEntity.getPartTypeId().intValue());
		if (removeStatus == 0) {
			logger.info("The PartTypes entity was successfully removed from the database");
		} else {
			logger.error("The PartTypes entity was not successfully removed from the database");
		}
		logger.info("OUT method removePartTypesEntity() in PartTypesDatabaseOperations");
	}

	public void updatePartTypesEntity(PartTypes partTypesEntity) {
		logger.info("IN method updatePartTypesEntity() in PartTypesDatabaseOperations");
		logger.info("Updating a PartTypes entity with the id: " + partTypesEntity.getPartTypeId());
		String query = "SELECT partTypesEntity FROM PartTypes partTypesEntity where partTypesEntity.partTypeId = "
				+ partTypesEntity.getPartTypeId().intValue();
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retreivedEntities = databaseAccessService.getEntities(query);
		boolean updated = false;
		for (int index = 0; index < retreivedEntities.size(); index++) {
			PartTypes currentEntity = (PartTypes) retreivedEntities.get(index);
			if (currentEntity.getPartTypeId().intValue() == partTypesEntity.getPartTypeId().intValue()) {
				// this is the entity to be updated
				currentEntity.setNote(partTypesEntity.getNote());
				currentEntity.setPartCode(partTypesEntity.getPartCode());
				int updateStatus = databaseAccessService.updateEntity(currentEntity);
				if (updateStatus == 0) {
					updated = true;
				} else {
					updated = false;
				}
				break;
			}
		}
		if (updated) {
			logger.info("The PartTypes entity was successfully updated");
		} else {
			logger.error("The PartTypes entity was not successfully updated");
		}
		logger.info("OUT method updatePartTypesEntity() in PartTypesDatabaseOperations");
	}

	public List<PartTypes> getAllPartTypesEntities() {
		logger.info("IN method getAllPartTypesEntities() in PartTypesDatabaseOperations");
		List<PartTypes> toReturnList = new ArrayList<PartTypes>();
		String query = "SELECT partTypesEntity FROM PartTypes partTypesEntity";
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrivedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrivedEntities.size(); index++) {
			PartTypes currentEntity = (PartTypes) retrivedEntities.get(index);
			toReturnList.add(currentEntity);
		}
		logger.info("OUT method getAllPartTypesEntities() in PartTypesDatabaseOperations");
		return toReturnList;
	}

	public List<PartTypes> getPartTypeEntitiesByPartTypeId(int partTypeId) {
		logger.info("IN method getPartTypeEntitiesByPartTypeId() in PartTypeDatabaseOperations");
		List<PartTypes> toReturnList = new ArrayList<PartTypes>();
		String query = "SELECT partTypesEntity FROM PartTypes partTypesEntity WHERE partTypesEntity.partTypeId = " + partTypeId;
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrivedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retrivedEntities.size(); index++) {
			PartTypes currentEntity = (PartTypes) retrivedEntities.get(index);
			toReturnList.add(currentEntity);
		}
		logger.info("OUT method getPartTypeEntitiesByPartTypeId() in PartTypeDatabaseOperations");
		return toReturnList;
	}

}
